package com.xiaotu.service;

import com.xiaotu.pojo.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class RabbitMQService {
    /**
     * Publish/Subscribe工作模式接收,处理邮件业务
     */
    @RabbitListener(bindings = @QueueBinding(value =
    @Queue("fanout_queue_email"),exchange =
    @Exchange(value = "fanout_exchange",type = "fanout")))
    public void psubConsumerEmailAno(User user){
        System.out.println(System.currentTimeMillis()+"邮箱业务接收到消息:"+user);
    }
     @RabbitListener(bindings = @QueueBinding(value = @Queue("fanout_queue_sms"),exchange = @Exchange(value = "fanout_exchange",type = "fanout")))
      public void psubConsumerSmsAno(User user){
         System.out.println(System.currentTimeMillis()+"短信业务接收到消息:"+user);
     }
    /**
     * 路由摸式消息模式  处理error级别日志信息
     */
    @RabbitListener(bindings = @QueueBinding(value =
            @Queue("routing_queue_error"),exchange =
            @Exchange(value = "routing_exchange",type = "direct"),
            key = "error_routing_key"
    ))
    public void routingConsumerErrsor(String message){
        System.out.println("接收error级别日志消息"+message);
    }
    @RabbitListener(bindings = @QueueBinding(value =
            @Queue("routing_queue_all"),exchange =
            @Exchange(value = "routing_exchange",type = "direct"),
            key = {"error_routing_key","info_routing_key","waring_routing_key"}
    ))
    public void routingConsumerAll(String message){
        System.out.println("接收到info,error,warning等级别日志消息"+message);
    }
    /**
     * 通配符模式消息接收,
     */
    @RabbitListener(bindings = @QueueBinding(value =
        @Queue("topic_queue_email"),exchange =
            @Exchange(value = "topic_exchange",type = "topic"),
            key = "info.#.email.#"
    ))
    public void topicConsumerEmail(String message){
        System.out.println(message);
    }
    @RabbitListener(bindings = @QueueBinding(value =
    @Queue("topic_queue_sms"),exchange =
    @Exchange(value = "topic_exchange",type = "topic"),
            key = "info.#.sms.#"
    ))
    public void topicConsumerSms(String message){
        System.out.println(message);
    }
    /*@RabbitListener(queues = "fanout_queue_email")
    public void psubConsumerEmail(Message message){
        byte[] body = message.getBody();
        String s = new String(body);
        System.out.println("邮件业务接收到的消息:"+s);
    }*/
   /* @RabbitListener(queues = "fanout_queue_sms")
    public void psubConsumerSms(Message message){
        byte[] body = message.getBody();
        String s = new String(body);
        System.out.println("短信业务接收到的消息:"+s);
    }*/
}
